package com.example.coinhelper.Manage;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.coinhelper.Bean.Details;
import com.example.coinhelper.Bean.User;
import com.example.coinhelper.db.DetailsDBHelper;
import com.example.coinhelper.db.UserDBHelper;

import java.util.ArrayList;
import java.util.List;

public class DetailsManage {
    private SQLiteOpenHelper dbHelper;
    public DetailsManage(Context context){
        dbHelper = new DetailsDBHelper(context);
    }
    public long addDetails(Details details){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(DetailsDBHelper.COLUMN_ICON,details.getIcon());
        values.put(DetailsDBHelper.COLUMN_MONEY,details.getMoney());
        values.put(DetailsDBHelper.COLUMN_DATE,details.getDate());
        values.put(DetailsDBHelper.COLUMN_USERNAME,details.getUsername());
        values.put(DetailsDBHelper.COLUMN_OPERATION,details.getOperation());
        values.put(DetailsDBHelper.COLUMN_OTHER,details.getOther());

        long id = db.insert(DetailsDBHelper.TABLE_DETAILS,null,values);
        db.close();
        return id;
    }
    @SuppressLint("Range")
    public List<Details> getAllDetils(){
        List<Details> detailsList = new ArrayList<Details>();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        Cursor cursor = db.query(DetailsDBHelper.TABLE_DETAILS, null, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Details details = new Details();
                // 从数据库中提取用户信息，并创建User对象
                details.setUsername(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_USERNAME)));
                details.setIcon(cursor.getInt(cursor.getColumnIndex(DetailsDBHelper.COLUMN_ICON)));
                details.setDate(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_DATE)));
                details.setMoney(cursor.getDouble(cursor.getColumnIndex(DetailsDBHelper.COLUMN_MONEY)));
                details.setOperation(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_OPERATION)));
                details.setOther(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_OTHER)));

                detailsList.add(details);
            } while (cursor.moveToNext());
            cursor.close();
        }
        db.close();
        return detailsList;
    }

    @SuppressLint("Range")
    public List<Details> getAllDetailsByUsername(String username) {
        List<Details> detailsList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String selection = DetailsDBHelper.COLUMN_USERNAME + "=?";
        String[] selectionArgs = { username };
        Cursor cursor = db.query(
                DetailsDBHelper.TABLE_DETAILS,
                null,
                selection,
                selectionArgs,
                null,
                null,
                DetailsDBHelper.COLUMN_DATE + " DESC" // 按时间列递减排序
        );
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Details details = new Details();
                // 从数据库中提取用户信息，并创建Details对象
                details.setUsername(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_USERNAME)));
                details.setId(cursor.getInt(cursor.getColumnIndex(DetailsDBHelper.COLUMN_ID)));
                details.setIcon(cursor.getInt(cursor.getColumnIndex(DetailsDBHelper.COLUMN_ICON)));
                details.setDate(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_DATE)));
                details.setMoney(cursor.getDouble(cursor.getColumnIndex(DetailsDBHelper.COLUMN_MONEY)));
                details.setOperation(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_OPERATION)));
                details.setOther(cursor.getString(cursor.getColumnIndex(DetailsDBHelper.COLUMN_OTHER)));

                detailsList.add(details);
            } while (cursor.moveToNext());
            cursor.close();
        }
        db.close();
        return detailsList;
    }

    public long deleteItemFromAccounttbById(long clickId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int i = db.delete(DetailsDBHelper.TABLE_DETAILS , DetailsDBHelper.COLUMN_ID+"=?" , new String[]{clickId+""});
        return i;
    }
    public long updateDetails(Details details) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DetailsDBHelper.COLUMN_USERNAME, details.getUsername());
        values.put(DetailsDBHelper.COLUMN_DATE, details.getDate());
        values.put(DetailsDBHelper.COLUMN_OPERATION, details.getOperation());
        values.put(DetailsDBHelper.COLUMN_MONEY, details.getMoney());
        values.put(DetailsDBHelper.COLUMN_ICON, details.getIcon());
        values.put(DetailsDBHelper.COLUMN_OTHER, details.getOther());

        String selection = DetailsDBHelper.COLUMN_ID + "=?";
        String[] selectionArgs = { String.valueOf(details.getId()) };

        long numRowsUpdated = db.update(DetailsDBHelper.TABLE_DETAILS, values, selection, selectionArgs);

        db.close();

        return numRowsUpdated;
    }

}
